<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/23
 * Time: 17:58
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\ProductService;
use App\Constant\ErrorConstant;

class ProductController extends BaseController
{
    protected $service;

    public function __construct(Request $request, ProductService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 产品列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listProduct()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query' =>  'array',
        ], [
            'query.array'   =>  '搜索条件必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_LIST_ERROR, $validator->errors()->first());
        }

        $query  =   $this->request->post('query', []);
        $page   =   (int) $this->request->input('page', 1);
        $size   =   (int) $this->request->input('page_size', 20);
        $name   =   $this->request->input('name', 'sorting');
        $sort   =   $this->request->input('sort', 'desc');

        if (!in_array($name, ['created_at', 'updated_at', 'real_sales', 'stock', 'sell_price', 'sorting'])) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_LIST_ERROR, '排序名称错误');
        }

        if (!in_array($sort, ['asc', 'desc'])) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_LIST_ERROR, '排序方式错误');
        }

        [$flag, $result]    =   $this->service->productList($query, $page, $size, $name, $sort);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_LIST_ERROR, $result);
        }
    }

    /**
     * 产品上下架操作
     * @return \Illuminate\Http\JsonResponse
     */
    public function offProduct()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'       =>  'required|integer|exists:mini_product,id',
            'shelf_status'  =>  'required|integer|between:0,1'
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整数',
            'good_id.exists'    =>  '商品不存在',
            'shelf_status.required' =>  '操作状态不能为空',
            'shelf_status.integer'  =>  '操作状态必须为整数',
            'shelf_status.between'  =>  '操作状态错误'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_OFF_ERROR, $validator->errors()->first());
        }

        $good_id     =  (int) $this->request->post('good_id');
        $shelf_status=  (int) $this->request->post('shelf_status');

        [$flag, $result]    =   $this->service->productOff($good_id, $shelf_status);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_OFF_ERROR, $result);
        }
    }

    /**
     * 产品详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailProduct()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'       =>  'required|integer',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_DETAIL_ERROR, $validator->errors()->first());
        }

        $good_id    =   (int) $this->request->post('good_id');

        [$flag, $result]    =   $this->service->productDetail($good_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_DETAIL_ERROR, $result);
        }
    }

    /**
     * 产品删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteProduct()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'       =>  'required|integer',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_DELETE_ERROR, $validator->errors()->first());
        }

        $good_id    =   (int) $this->request->post('good_id');

        [$flag, $result]    =   $this->service->productDelete($good_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_DELETE_ERROR, $result);
        }
    }

    /**
     * 产品添加
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function addProduct()
    {
        $validator  =   Validator::make($this->request->all(), [
            'name'      =>  'required|string|max:255',
            'shop_id'   =>  'required|integer|exists:mini_shop,id',
            'brand_id'  =>  'required|integer|exists:mini_brand,id',
            'supply_id' =>  'required|integer|exists:mini_supply,id',
            'sku_code'  =>  'required|string|max:50',
            'is_shelf'  =>  'required|integer|between:0,1',
//            'is_hot'    =>  'required|integer|between:0,1',
            'sorting'   =>  'required|integer',
            'stock'     =>  'required|integer',
            'unit'      =>  'string|max:30',
            'weight'    =>  'string|max:30',
            'desc'      =>  'string|max:1000',
            'detail'    =>  'string',
            'spec_remark'   =>  'string|max:500',
            'is_commission' =>  'required|integer|between:0,1',
            'is_box_commission' =>  'required|integer|between:0,1',
            'is_auto_send'  =>  'required|integer|between:0,1',
            'reduce_type'   =>  'required|integer|between:1,2',
//            'sell_price'    =>  'required|numeric',
//            'cost_price'    =>  'required|numeric',
            'limit'         =>  'required|integer',
            'virtual'       =>  'required|integer',
            'original_price'=>  'required|numeric',
//            'cover_pic'     =>  'required|image|max:5120',
            'cover_pic'     =>  'required|array',
            'cover_pic.*'   =>  'required|string',
//            'cover_video'   =>  'mimetypes:video/avi,video/mpeg,video/quicktime',
            'cover_video'   =>  'string',
            'cat_id'        =>  'required|array',
            'cat_id.*'      =>  'required|integer|exists:mini_category,id',
            'good_detail'   =>  'required|array',
//            'good_detail.*' =>  'required|image|max:5120',
            'good_detail.*' =>  'required|string',
            'good_spec'     =>  'required|array',
            'good_spec.*.attr_id'   =>  'required|integer|exists:mini_attribute,id',
            'good_spec.*.attr_value'=>  'required|string',
            'good_spec.*.sell_price'=>  'required|numeric',
            'good_spec.*.original_price'=>  'required|numeric',
            'good_spec.*.cost_price'=>  'required|numeric',
            'good_spec.*.sku_code'  =>  'required|string|max:50',
            'good_spec.*.stock'     =>  'required|integer',
//            'good_spec.*.spec_pic'  =>  'required|image|max:5120',
            'good_spec.*.spec_pic'  =>  'required|string',
        ], [
            'name.required'     =>  '商品名称不能为空',
            'shop_id.required'  =>  '店铺ID不能为空',
            'brand_id.required' =>  '品牌ID不能为空',
            'supply_id.required'=>  '供应商ID不能为空',
            'sku_code.required' =>  '商品编码不能为空',
            'is_shelf.required' =>  '是否售卖不能为空',
            'is_hot.required'   =>  '是否热门不能为空',
            'sorting.required'  =>  '商品排序不能为空',
            'stock.required'    =>  '商品库存不能为空',
            'is_commission.required'    =>  '是否分佣不能为空',
            'is_box_commission.required'=>  '是否盒子分佣不能为空',
            'is_auto_send.required'     =>  '是否自动发货不能为空',
            'reduce_type.required'      =>  '减库存方式不能为空',
            'sell_price.required'       =>  '售卖价格不能为空',
            'cost_price.required'       =>  '成本价格不能为空',
            'limit.required'            =>  '限购数量不能为空',
            'virtual.required'          =>  '虚拟销量不能为空',
            'original_price.required'   =>  '商品原价不能为空',
            'cat_id.required'           =>  '分类选择不能为空',
            'cover_pic.required'        =>  '封面图不能为空',
            'cover_pic.array'           =>  '封面图必须为数组',
            'cover_pic.*.required'      =>  '封面图必须为字符',
            'cover_pic.*.string'        =>  '封面图必须为字符',
            'good_detail.required'      => '商品详情图不能为空',
            'good_spec.required'        =>  '商品规格不能为空',
            /*'cover_pic.image'   =>  '封面图必须为图片',
            'cover_pic.max'     =>  '封面图过大',*/

            'name.string'       =>  '商品名称必须为字符',
            'sku_code.string'   =>  '商品编码必须为字符',
            'unit.string'       =>  '商品单位必须为字符',
            'weight.string'     =>  '商品重量必须为字符',
            'desc.string'       =>  '商品描述必须为字符',
            'detail.string'     =>  '商品详情必须为字符',
            'spec_remark.string'=>  '特殊说明必须为字符',

            'shop_id.integer'   =>  '店铺ID必须为整数',
            'brand_id.integer'  =>  '品牌ID必须为整数',
            'supply_id.integer' =>  '供应商ID必须为整数',
            'is_shelf.integer'  =>  '是否售卖必须为整数',
            'is_hot.integer'    =>  '是否热门必须为整数',
            'sorting.integer'   =>  '商品排序必须为整数',
            'stock.integer'     =>  '商品库存必须为整数',
            'is_commission.integer'     =>  '是否分佣必须为整数',
            'is_box_commission.integer' =>  '是否盒子分佣必须为整数',
            'is_auto_send.integer'      =>  '是否自动发货必须为整数',
            'reduce_type.integer'       =>  '减库存方式必须为整数',
            'limit.integer'     =>  '限购数量必须为整数',
            'virtual.integer'   =>  '虚拟销量必须为整数',

            'shop_id.exists'    =>  '店铺不存在',
            'brand_id.exists'   =>  '品牌不存在',
            'supply_id.exists'  =>  '供应商不存在',
            'sku_code.max'      =>  '商品编码过长',
            'unit.max'          =>  '商品单位过长',
            'weight.max'        =>  '商品重量过长',
            'desc.max'          =>  '商品描述过长',
            'detail.max'        =>  '商品详情过长',
            'spec_remark.max'   =>  '特殊说明过长',
            'is_shelf.between'  =>  '是否售卖选择错误',
            'is_hot.between'    =>  '是否热卖选择错误',
            'is_commission.between'     =>  '是否分佣选择错误',
            'is_box_commission.between' =>  '是否盒子分佣选择错误',
            'is_auto_send.between'  =>  '是否自动发货选择错误',
            'reduce_type.between'   =>  '减库存方式选择错误',

            'sell_price.numeric'    =>  '售卖价格必须为数字',
            'cost_price.numeric'    =>  '成本价格必须为数字',
            'original_price.numeric'=>  '商品原价必须为数字',
            'cat_id.array'          =>  '分类选择必须为数组',
            'good_detail.array'     =>  '商品详情图必须为数组',
            'good_spec.array'       =>  '商品规格必须为数组',
//            'cover_video.mimetypes' =>  '上传视频格式错误',
            'cover_video.string'    =>  '上传视频必须为字符',

            'cat_id.*.required'     =>  '分类ID不能为空',
            'cat_id.*.integer'      =>  '分类ID必须为整数',
            'cat_id.*.exists'       =>  '分类ID不存在',

            'good_spec.*.attr_id.required' => '商品规格属性ID不能为空',
            'good_spec.*.attr_id.integer'  => '商品规格属性ID必须为整数',
            'good_spec.*.attr_id.exists'   => '商品规格属性ID不存在',

            'good_spec.*.attr_value.required' => '商品规格属性值不能为空',
            'good_spec.*.attr_value.max'      => '商品规格属性值过长',

            'good_spec.*.sell_price.required'   => '商品规格售卖价格不能为空',
            'good_spec.*.original_price.required'=>'商品规格原价不能为空',
            'good_spec.*.cost_price.required'   => '商品规格成本价不能为空',
            'good_spec.*.sku_code.required'     => '商品规格编码不能为空',
            'good_spec.*.spec_pic.required'     => '商品规格图片不能为空',
            'good_spec.*.stock.required'        => '商品规格库存不能为空',

            'good_spec.*.sell_price.numeric'    => '商品规格售卖价格必须为数字',
            'good_spec.*.original_price.numeric'=> '商品规格原价必须为数字',
            'good_spec.*.cost_price.numeric'    => '商品规格成本价必须为数字',
            'good_spec.*.stock.integer'         => '商品规格成本价必须为整数',

            'good_spec.*.sku_code.max'          => '商品规格编码过长',
            /*'good_spec.*.spec_pic.image'        => '商品规格图片格式错误',
            'good_spec.*.spec_pic.max'          => '商品规格图片过大',*/
            'good_spec.*.spec_pic.string'       => '商品规格必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->productAdd($this->request->all());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_ADD_ERROR, $result);
        }
    }

    /**
     * 产品编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function editProduct()
    {
        $validator  =   Validator::make($this->request->all(), [
            'good_id'   =>  'required|integer|exists:mini_product,id',
            'name'      =>  'required|string|max:255',
            'shop_id'   =>  'required|integer|exists:mini_shop,id',
            'brand_id'  =>  'required|integer|exists:mini_brand,id',
            'supply_id' =>  'required|integer|exists:mini_supply,id',
            'sku_code'  =>  'required|string|max:50',
            'is_shelf'  =>  'required|integer|between:0,1',
            'is_hot'    =>  'required|integer|between:0,1',
            'sorting'   =>  'required|integer',
            'stock'     =>  'required|integer',
            'unit'      =>  'string|max:30',
            'weight'    =>  'string|max:30',
            'desc'      =>  'string|max:1000',
            'detail'    =>  'string',
            'spec_remark'   =>  'string|max:500',
            'is_commission' =>  'required|integer|between:0,1',
            'is_box_commission' =>  'required|integer|between:0,1',
            'is_auto_send'  =>  'required|integer|between:0,1',
            'reduce_type'   =>  'required|integer|between:1,2',
            'sell_price'    =>  'required|numeric',
            'cost_price'    =>  'required|numeric',
            'limit'         =>  'required|integer',
            'virtual'       =>  'required|integer',
            'original_price'=>  'required|numeric',
            'cover_pic'     =>  'required|array',
            'cover_pic.*'   =>  'required|string',
            'cover_video'   =>  'string',
            'cat_id'        =>  'required|array',
            'cat_id.*'      =>  'required|integer|exists:mini_category,id',
            'good_detail'   =>  'required|array',
            'good_detail.*' =>  'required|string',
            'good_spec'     =>  'required|array',
            'good_spec.*.attr_id'   =>  'required|integer|exists:mini_attribute,id',
            'good_spec.*.attr_value'=>  'required|string',
            'good_spec.*.sell_price'=>  'required|numeric',
            'good_spec.*.original_price'=>  'required|numeric',
            'good_spec.*.cost_price'=>  'required|numeric',
            'good_spec.*.sku_code'  =>  'required|string|max:50',
            'good_spec.*.stock'     =>  'required|integer',
            'good_spec.*.spec_pic'  =>  'required|string',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整数',
            'good_id.exists'    =>  '商品不存在',

            'name.required'     =>  '商品名称不能为空',
            'shop_id.required'  =>  '店铺ID不能为空',
            'brand_id.required' =>  '品牌ID不能为空',
            'supply_id.required'=>  '供应商ID不能为空',
            'sku_code.required' =>  '商品编码不能为空',
            'is_shelf.required' =>  '是否售卖不能为空',
            'is_hot.required'   =>  '是否热门不能为空',
            'sorting.required'  =>  '商品排序不能为空',
            'stock.required'    =>  '商品库存不能为空',
            'is_commission.required'    =>  '是否分佣不能为空',
            'is_box_commission.required'=>  '是否盒子分佣不能为空',
            'is_auto_send.required'     =>  '是否自动发货不能为空',
            'reduce_type.required'      =>  '减库存方式不能为空',
            'sell_price.required'       =>  '售卖价格不能为空',
            'cost_price.required'       =>  '成本价格不能为空',
            'limit.required'            =>  '限购数量不能为空',
            'virtual.required'          =>  '虚拟销量不能为空',
            'original_price.required'   =>  '商品原价不能为空',
            'cat_id.required'           =>  '分类选择不能为空',
            'cover_pic.required'        =>  '封面图不能为空',
            'cover_pic.array'           =>  '封面图必须为数组',
            'cover_pic.*.required'      =>  '封面图必须为字符',
            'cover_pic.*.string'        =>  '封面图必须为字符',
            'good_detail.required'      => '商品详情图不能为空',
            'good_spec.required'        =>  '商品规格不能为空',

            'name.string'       =>  '商品名称必须为字符',
            'sku_code.string'   =>  '商品编码必须为字符',
            'unit.string'       =>  '商品单位必须为字符',
            'weight.string'     =>  '商品重量必须为字符',
            'desc.string'       =>  '商品描述必须为字符',
            'detail.string'     =>  '商品详情必须为字符',
            'spec_remark.string'=>  '特殊说明必须为字符',

            'shop_id.integer'   =>  '店铺ID必须为整数',
            'brand_id.integer'  =>  '品牌ID必须为整数',
            'supply_id.integer' =>  '供应商ID必须为整数',
            'is_shelf.integer'  =>  '是否售卖必须为整数',
            'is_hot.integer'    =>  '是否热门必须为整数',
            'sorting.integer'   =>  '商品排序必须为整数',
            'stock.integer'     =>  '商品库存必须为整数',
            'is_commission.integer'     =>  '是否分佣必须为整数',
            'is_box_commission.integer' =>  '是否盒子分佣必须为整数',
            'is_auto_send.integer'      =>  '是否自动发货必须为整数',
            'reduce_type.integer'       =>  '减库存方式必须为整数',
            'limit.integer'     =>  '限购数量必须为整数',
            'virtual.integer'   =>  '虚拟销量必须为整数',

            'shop_id.exists'    =>  '店铺不存在',
            'brand_id.exists'   =>  '品牌不存在',
            'supply_id.exists'  =>  '供应商不存在',
            'sku_code.max'      =>  '商品编码过长',
            'unit.max'          =>  '商品单位过长',
            'weight.max'        =>  '商品重量过长',
            'desc.max'          =>  '商品描述过长',
            'detail.max'        =>  '商品详情过长',
            'spec_remark.max'   =>  '特殊说明过长',
            'is_shelf.between'  =>  '是否售卖选择错误',
            'is_hot.between'    =>  '是否热卖选择错误',
            'is_commission.between'     =>  '是否分佣选择错误',
            'is_box_commission.between' =>  '是否盒子分佣选择错误',
            'is_auto_send.between'  =>  '是否自动发货选择错误',
            'reduce_type.between'   =>  '减库存方式选择错误',

            'sell_price.numeric'    =>  '售卖价格必须为数字',
            'cost_price.numeric'    =>  '成本价格必须为数字',
            'original_price.numeric'=>  '商品原价必须为数字',
            'cat_id.array'          =>  '分类选择必须为数组',
            'good_detail.array'     =>  '商品详情图必须为数组',
            'good_spec.array'       =>  '商品规格必须为数组',
//            'cover_video.mimetypes' =>  '上传视频格式错误',
            'cover_video.string'    =>  '上传视频必须为字符',

            'cat_id.*.required'     =>  '分类ID不能为空',
            'cat_id.*.integer'      =>  '分类ID必须为整数',
            'cat_id.*.exists'       =>  '分类ID不存在',

            'good_spec.*.attr_id.required' => '商品规格属性ID不能为空',
            'good_spec.*.attr_id.integer'  => '商品规格属性ID必须为整数',
            'good_spec.*.attr_id.exists'   => '商品规格属性ID不存在',

            'good_spec.*.attr_value.required' => '商品规格属性值不能为空',
            'good_spec.*.attr_value.max'      => '商品规格属性值过长',

            'good_spec.*.sell_price.required'   => '商品规格售卖价格不能为空',
            'good_spec.*.original_price.required'=>'商品规格原价不能为空',
            'good_spec.*.cost_price.required'   => '商品规格成本价不能为空',
            'good_spec.*.sku_code.required'     => '商品规格编码不能为空',
            'good_spec.*.spec_pic.required'     => '商品规格图片不能为空',
            'good_spec.*.stock.required'        => '商品规格库存不能为空',

            'good_spec.*.sell_price.numeric'    => '商品规格售卖价格必须为数字',
            'good_spec.*.original_price.numeric'=> '商品规格原价必须为数字',
            'good_spec.*.cost_price.numeric'    => '商品规格成本价必须为数字',
            'good_spec.*.stock.integer'         => '商品规格成本价必须为整数',

            'good_spec.*.sku_code.max'          => '商品规格编码过长',
            'good_spec.*.spec_pic.string'       => '商品规格必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_ADD_ERROR, $validator->errors()->first());
        }

        $params     =   $this->request->post();
        $good_id    =   (int) $this->request->post('good_id');
        [$flag, $result]    =   $this->service->productEdit($params, $good_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_ADD_ERROR, $result);
        }
    }

    /**
     * 商品列表生成二维码
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function codeProduct()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'required|integer|exists:mini_product,id',
            'box_id'    =>  'required|integer|exists:mini_box,id',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整数',
            'good_id.exists'    =>  '商品不存在',
            'box_id.required'   =>  '商品ID不能为空',
            'box_id.integer'    =>  '商品ID必须为整数',
            'box_id.exists'     =>  '商品不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_CODE_ERROR, $validator->errors()->first());
        }

        $good_id    =   (int) $this->request->post('good_id');
        $box_id     =   (int) $this->request->post('box_id');
        [$flag, $result]    =   $this->service->productCode($good_id, $box_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_GOODS_ADD_ERROR, $result);
        }
    }

}
